//
//  HomeCenterPage-Slider.swift
//  Uber
//
//  Created by chen on 16/3/14.
//  Copyright © 2016年 chen. All rights reserved.
//

import UIKit

import Foundation


class CarAnimationView: BMKAnnotationView {
    var imageView:UIImageView!
    
    override init!(annotation: BMKAnnotation!, reuseIdentifier: String!) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        
        self.bounds = CGRect(x: 0, y: 0, width: 32, height: 32)
        self.backgroundColor = UIColor.clearColor()
        
        imageView = UIImageView(image: UIImage(named: "map-taxi.png"))
        imageView.frame = self.bounds
        self.addSubview(imageView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension HomeCenterPage {
    @IBAction func doShowLeft(){
        
        if showingLeft! {
            showCenterPanel()
        }
        else {
            showLeftPanel()
        }
    }
    
    func doTapCenter(sender:UIButton)
    {
        if showingLeft! {
            showCenterPanel()
        }
    }
    
    func addUserCenterView()
    {
        centerPage = UserCenterPage()
        
        centerPage!.owner = self;
        centerPage!.view.frame = leftView!.bounds
        
        leftView?.addSubview(centerPage!.view)
    }
    
    func doShowSetting()
    {
        showCenterPanel()
        self.navigationController!.view.sendSubviewToBack(backControl)
        
        let page = SettingPage()
        let navPage = UINavigationController(rootViewController: page)
        
        self.presentViewController(navPage, animated: true, completion: nil)
    }
    
    func addLeftView() {
        if leftView == nil {
            var frame:CGRect! = self.navigationController!.view.bounds
            
            frame.origin.x = -frame.size.width+PanWidth
            frame.size.width -= PanWidth
            
            leftView = UIView(frame: frame)
            leftView?.backgroundColor = UIColor.blackColor()
            self.navigationController?.view.addSubview(leftView!)
        }
    }
    
    func addBackControl() {
        if backControl == nil {
            let frame = self.navigationController!.view.bounds;
            backControl = UIControl(frame: frame)
            backControl.backgroundColor = UIColor.whiteColor()
            backControl.alpha = 0.2
            backControl.addTarget(self, action: Selector("doTapCenter:"), forControlEvents: .TouchUpInside);
            
            self.navigationController!.view.addSubview(backControl)
            self.navigationController!.view.sendSubviewToBack(backControl)
        }
    }
    
    func showCenterView(view:UIView,offset:CGFloat,shadow:Bool) {
        if shadow {
            view.layer.shadowColor = UIColor(white: 1.0, alpha: 0.15).CGColor
            view.layer.shadowOpacity = 0.3
            view.layer.shadowOffset = CGSizeMake(offset, offset)
        }
        else {
            view.layer.cornerRadius = 0.0
            view.layer.shadowOpacity = 0.0
            view.layer.shadowOffset = CGSizeMake(offset, offset)
        }
    }
    
    func showLeftPanel() {
        showingLeft = true
        showCenterView(leftView!, offset: -2, shadow: true)
        
        UIView.animateWithDuration(1.0, delay: 0, options: .BeginFromCurrentState, animations: {
            var frame = self.leftView?.frame
            frame?.origin.x = 0
            self.leftView?.frame = frame!
            
            frame = self.navigationController?.navigationBar.frame
            frame?.origin.x = (frame?.size.width)! - PanWidth
            self.navigationController?.navigationBar.frame = frame!
            }, completion: {(fished:Bool) in
                if fished {
                    self.navigationController!.view.bringSubviewToFront(self.backControl)
                    self.navigationController!.view.bringSubviewToFront(self.leftView!)
                }
                
        });
    }
    
    func showCenterPanel() {
        showingLeft = false
        
        UIView.animateWithDuration(1.0, delay: 0, options: .BeginFromCurrentState, animations: {
            var frame = self.leftView?.frame
            frame?.origin.x = -frame!.size.width
            self.leftView?.frame = frame!
            
            frame = self.navigationController?.navigationBar.frame
            frame?.origin.x = 0
            self.navigationController?.navigationBar.frame = frame!
            }, completion: {(fished:Bool) in
                if fished {
                    self.navigationController!.view.sendSubviewToBack(self.backControl)
                    self.showCenterView(self.leftView!, offset: 0, shadow: false)
                }
        });
        
    }
    //BaiduMap相关的方法类放在这个位置
    func addBaiduMap(){
        mapView = BMKMapView(frame: UIScreen.mainScreen().bounds)
        self.view.addSubview(mapView)
        mapView.zoomLevel = 12
        startLocation()
    }
    func startLocation(){
        locService = BMKLocationService()
        locService.startUserLocationService()
        mapView.showsUserLocation = true
        
        userCoordinate = CLLocationCoordinate2D(latitude: 39.915, longitude: 116.404)
        
    }
    func didUpdateBMKUserLocation(userLocation: BMKUserLocation!) {
//        userCoordinate = userLocation.location.coordinate
//        print(userCoordinate.latitude)
//        mapView.updateLocationData(userLocation)
        addCars()
    }
    func addCars()
    {
        mapView?.removeOverlays(mapView?.overlays)
        if point != nil {
            mapView?.removeAnnotation(point)
        }
        
        point = BMKPointAnnotation()
        
        point!.coordinate = CLLocationCoordinate2D(latitude: userCoordinate!.latitude+0.002, longitude: userCoordinate!.longitude+0.002)
        point!.title="Car"
        mapView?.addAnnotation(point!)
    }
    //显示当前位置的图标信息
    func mapView(mapView: BMKMapView!, viewForAnnotation annotation: BMKAnnotation!) -> BMKAnnotationView! {
//        let car = BMKPinAnnotationView(annotation: annotation, reuseIdentifier: "PID")
//        car.image = UIImage(named: "map-taxi.png")
//        car.draggable = false
//        return car
        let car = CarAnimationView(annotation: annotation, reuseIdentifier: "PID")
        car.draggable = false
        return car
    }
    
}